package com.example.demo.controller;


import com.example.demo.Service.CommentService;
import com.example.demo.entity.Comment;
import com.example.demo.entity.Post;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/comments")
public class CommentController {
    @Autowired
    private CommentService commentService;

    //获取所有评论
    @GetMapping
    public List<Comment> getAllComments() {
        return commentService.findAll();
    }

    //根据Post获取相应的评论
    // 根据Post ID获取相应的评论
    @GetMapping("/posts/{postid}/comments")
    public List<Comment> getCommentsByPost(@PathVariable Long postid){
        Post post = new Post();
        post.setId(postid);
        return commentService.findCommentsByPost(post);
    }

    //根据username获取相应的评论
    @GetMapping("/user/{username}")
    public List<Comment> getCommentsByUser(@PathVariable String username) {
        return commentService.findCommentsByUser(username);
    }

    //根据ID获取相应的评论
    @GetMapping("/userId/{userId}")
    public List<Comment> getCommentsByUserId(@PathVariable Long userId) {
        return commentService.findCommentsByUserId(userId);
    }

    //创建评论
    @PostMapping
    public Comment createComment(@RequestBody Comment comment) {
        return commentService.save(comment);
    }

    //删除评论
    @DeleteMapping("/{id}")
    public void deleteComment(@PathVariable Long id) {
        commentService.deleteById(id);
    }

}
